package com.txzt.gaea.business.device.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.business.device.domain.model.DeviceUse;
import com.txzt.gaea.business.device.service.IDeviceUseService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.enums.DeviceType;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/device/use")
public class DeviceUseController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(DeviceUseController.class);

    @Autowired
    private IDeviceUseService deviceUseService;


    /**
     * 分页查询个人设备领用
     * @param pageNumber
     * @param pageSize
     * @param deviceUse
     * @return
     */
    @GetMapping(value = "/listPersonal")
    public RtnInfo<?> listPersonal(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, DeviceUse deviceUse) {
        logger.info(deviceUse.toString());
        deviceUse.setDeviceType(DeviceType.PERSONAL.getCode());
        PageQuery<DeviceUse> pageList = deviceUseService.pageQuery(new PageQuery<>(pageNumber, pageSize, deviceUse));
        pageList.getList().forEach(item -> {
            //翻译部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * 分页查询公用设备领用
     * @param pageNumber
     * @param pageSize
     * @param deviceUse
     * @return
     */
    @GetMapping(value = "/listPublic")
    public RtnInfo<?> listPublic(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, DeviceUse deviceUse) {
        logger.info(deviceUse.toString());
        deviceUse.setDeviceType(DeviceType.PUBLIC.getCode());
        PageQuery<DeviceUse> pageList = deviceUseService.pageQuery(new PageQuery<>(pageNumber, pageSize, deviceUse));
        pageList.getList().forEach(item -> {
            //翻译部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * 分页查询保密设备领用
     * @param pageNumber
     * @param pageSize
     * @param deviceUse
     * @return
     */
    @GetMapping(value = "/listSecrecy")
    public RtnInfo<?> listSecrecy(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, DeviceUse deviceUse) {
        logger.info(deviceUse.toString());
        deviceUse.setDeviceType(DeviceType.SECRECY.getCode());
        PageQuery<DeviceUse> pageList = deviceUseService.pageQuery(new PageQuery<>(pageNumber, pageSize, deviceUse));
        pageList.getList().forEach(item -> {
            //翻译部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * id查询设备领用
     * @param pageNumber
     * @param pageSize
     * @param deviceUse
     * @return
     */
    @GetMapping(value = "/list")
    public RtnInfo<?> listByDeviceId(@RequestParam(defaultValue = "1") Integer pageNumber,
                                  @RequestParam(defaultValue = "10") Integer pageSize, DeviceUse deviceUse) {
        logger.info(deviceUse.toString());
        PageQuery<DeviceUse> pageList = deviceUseService.pageQuery(new PageQuery<>(pageNumber, pageSize, deviceUse));
        pageList.getList().forEach(item -> {
            //翻译部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * 新增个人设备领用
     * @param deviceUse
     * @return
     */
    @PostMapping(value = "/addPersonal")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> addPersonal(@RequestBody DeviceUse deviceUse) {
        deviceUse.setDeviceType(DeviceType.PERSONAL.getCode());
        int count = deviceUseService.addDeviceUse(deviceUse);
        if (count == 99) {
            return RtnInfo.error(99,"领用设备大于库存可用数量，领用失败！");
        }
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 新增公用设备领用
     * @param deviceUse
     * @return
     */
    @PostMapping(value = "/addPublic")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> addPublic(@RequestBody DeviceUse deviceUse) {
        deviceUse.setDeviceType(DeviceType.PUBLIC.getCode());
        int count = deviceUseService.addDeviceUse(deviceUse);
        if (count == 99) {
            return RtnInfo.error(99,"领用设备大于库存可用数量，领用失败！");
        }
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 新增保密设备领用
     * @param deviceUse
     * @return
     */
    @PostMapping(value = "/addSecrecy")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> addSecrecy(@RequestBody DeviceUse deviceUse) {
        deviceUse.setDeviceType(DeviceType.SECRECY.getCode());
        int count = deviceUseService.addDeviceUse(deviceUse);
        if (count == 99) {
            return RtnInfo.error(99,"领用设备大于库存可用数量，领用失败！");
        }
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 设备归还
     * @param id
     * @return
     */
    @GetMapping(value = "/return")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> returnDevice(@RequestParam Long id) {
        int count = deviceUseService.returnDevice(id);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delete(@RequestParam Long id) {
        int count = deviceUseService.deleteDeviceUse(id);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

}
